package hit.example.net;

import hit.example.clipbroad.ActivityClipbroad;
import android.annotation.SuppressLint;
import android.annotation.TargetApi;
import android.content.ClipDescription;
import android.content.ClipboardManager;
import android.content.ClipboardManager.OnPrimaryClipChangedListener;
import android.content.Context;
import android.content.Intent;
import android.os.Build;
import android.os.IBinder;
import android.util.Log;
import android.widget.Toast;

@SuppressLint("DefaultLocale")
@TargetApi(Build.VERSION_CODES.HONEYCOMB)
public class MyService extends android.app.Service {

	/*
	 * private OnPrimaryClipChangedListener listener = new
	 * OnPrimaryClipChangedListener() { public void onPrimaryClipChanged() {
	 * 
	 * 
	 * } };
	 */
	String str;

	@SuppressWarnings("deprecation")
	@TargetApi(Build.VERSION_CODES.HONEYCOMB)
	@Override
	public void onCreate() {
		Log.i("dinhtv", "oncreat service");
		int verSDK = android.os.Build.VERSION.SDK_INT;
		if (verSDK < android.os.Build.VERSION_CODES.HONEYCOMB) {
			android.text.ClipboardManager clipboard = (android.text.ClipboardManager) getSystemService(Context.CLIPBOARD_SERVICE);

			Toast.makeText(getApplicationContext(),
					"Version thấp ! Không chạy được service Clipbroad",
					Toast.LENGTH_SHORT).show();
			str = (String) clipboard.getText();
			if (str != clipboard.getText()) {

			}

		} else {
			android.content.ClipboardManager clipboard = (android.content.ClipboardManager) getSystemService(Context.CLIPBOARD_SERVICE);

			Toast.makeText(getApplicationContext(), "ban moi",
					Toast.LENGTH_SHORT).show();
			clipboard
					.addPrimaryClipChangedListener(new OnPrimaryChangedListener());
		}
		/*
		 * ((ClipboardManager) getSystemService(CLIPBOARD_SERVICE))
		 * .addPrimaryClipChangedListener(listener);
		 */
		super.onCreate();

	}

	@Override
	public int onStartCommand(Intent intent, int flags, int startId) {
		Log.i("dinhtv", "conmand");
		return START_STICKY;
	}

	@Override
	@Deprecated
	public void onStart(Intent intent, int startId) {

		super.onStart(intent, startId);
		Log.i("dinhtv", "Start service");
	}

	@Override
	public void onDestroy() {

		super.onDestroy();
	}

	@Override
	public IBinder onBind(Intent arg0) {
		return null;
	}

	public String readFromClipboard() {
		ClipboardManager clipboard = (ClipboardManager) getSystemService(Context.CLIPBOARD_SERVICE);
		if (clipboard.hasPrimaryClip()) {
			android.content.ClipDescription description = clipboard
					.getPrimaryClipDescription();
			android.content.ClipData data = clipboard.getPrimaryClip();
			if (data != null
					&& description != null
					&& description
							.hasMimeType(ClipDescription.MIMETYPE_TEXT_PLAIN))
				return String.valueOf(data.getItemAt(0).getText());
		}
		return null;
	}

	class OnPrimaryChangedListener implements OnPrimaryClipChangedListener {

		@Override
		public void onPrimaryClipChanged() {
			String str = readFromClipboard();

			String selection = "";

			StringBuilder sb = new StringBuilder();
			sb.append("word IN ('");

			String[] details = str.split(" ");

			for (int i = 0; i < details.length; i++) {
				if (i == details.length - 1) {
					sb.append(details[i] + "')");
				} else {

					sb.append(details[i] + "','");
				}
			}

			selection = sb.toString().toLowerCase();
			Intent intent = new Intent(getApplicationContext(),
					ActivityClipbroad.class);
			intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
			intent.putExtra("text", selection);
			startActivity(intent);
		}

	}
}